package com.yami.shop.platform.controller;

import com.yami.shop.bean.dto.FriendDTO;
import com.yami.shop.bean.model.MeltCloudUser;
import com.yami.shop.bean.vo.MyFriendVo;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.dto.user.RegisterRongReqDTO;
import com.yami.shop.security.platform.model.YamiSysUser;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.FriendService;
import com.yami.shop.service.impl.RongYunUserServiceImpl;
import com.yami.shop.service.impl.RongYunUserWhiteServiceImpl;
import com.yami.shop.sys.common.model.SysUser;
import com.yami.shop.sys.common.service.SysUserService;
import com.yami.shop.vo.user.UserBlackVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.yami.shop.common.response.ServerResponseEntity.success;

@RestController
@RequestMapping("/admin/melt/user")
@Tag(name = "融云用户管理")
public class MeltCloudUserController {


    @Autowired
    private FriendService friendService;

    @Autowired
    private RongYunUserWhiteServiceImpl whiteService;

    @Autowired
    private RongYunUserServiceImpl userService;
    
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/list")
    @Operation(summary =  "黑名单好友下拉", description = "黑名单好友下拉")
    public ServerResponseEntity<List<MyFriendVo>> list(@RequestBody FriendDTO friendDTO) {
        List<MyFriendVo> list = friendService.firendList(friendDTO.getUserId(), friendDTO.getUserName());
        return success(list);
    }

    @GetMapping("/queryBlacklist")
    @Operation(summary =  "黑名单列表", description = "黑名单列表")
    public ServerResponseEntity<PageVO<MeltCloudUser>> queryBlacklist(PageDTO pageDTO, @RequestParam("userId") String userId, @RequestParam(name="userName", required = false) String userName) throws Exception {
        return whiteService.queryBlacklist(pageDTO, userId, userName);
    }

    @PostMapping("/userBlacklistAdd")
    @Operation(summary =  "添加用户黑名单")
    public ServerResponseEntity<String> create(@RequestBody @Valid UserBlackVo groupVo) throws Exception {
        return whiteService.userBlacklistAdd(groupVo.getUserId(), groupVo.getUserBlacklist());
    }

    @PostMapping("/removeBlackList")
    @Operation(summary =  "解除用户黑名单")
    public ServerResponseEntity<String> removeBlackList(@RequestBody @Valid UserBlackVo groupVo) throws Exception {
        return whiteService.remove(groupVo.getUserId(), groupVo.getUserBlacklist());
    }

    @PostMapping("/refershUserInfo")
    @Operation(summary =  "刷新用户信息")
    public ServerResponseEntity<String> removeBlackList(@RequestBody @Valid RegisterRongReqDTO groupVo) throws Exception {
        return userService.userRefresh(groupVo);
    }

    @GetMapping("/getUserInfo")
    @Operation(summary =  "融云用户基本信息", description = "融云用户信息方法")
    public ServerResponseEntity<MeltCloudUser> getUserInfo(@RequestParam String userId) throws Exception {
        Long current = SecurityUtils.getSysUser().getUserId();
        return userService.getUserInfo(userId, null);
    }

    @GetMapping("/getUserList")
    @Operation(summary =  "融云用户列表", description = "融云用户列表")
    public ServerResponseEntity<PageVO<MeltCloudUser>> page(PageDTO pageDTO, @RequestParam(name="userName", required = false) String userName) throws Exception {
        return ServerResponseEntity.success(userService.userAll(pageDTO, userName));
    }

    @GetMapping("/abandon")
    @Operation(summary =  "注销用户", description = "注销用户")
    public ServerResponseEntity<String> abandon(@RequestParam String userId) throws Exception {
        return userService.abandon(userId);
    }

    @GetMapping("/token")
    @Operation(summary =  "获取融云用户token", description = "获取融云用户token")
    public ServerResponseEntity getToken() throws Exception {
        Long userId = SecurityUtils.getSysUser().getUserId();
        SysUser sysUser = sysUserService.getById(userId);
        RegisterRongReqDTO build = RegisterRongReqDTO.builder().id("plat_" + userId).name(sysUser.getNickName()).portrait("/2022/12/123.png").build();
        return userService.userRegister(build);
    }

}
